<?php
/**
 * @description: admin user model
 * @author sunnyw<iwisunny@gmail.com>
 */
class User_model extends MY_Model
{

    protected $_table='users';
    protected $soft_delete_key='is_delete';
    protected $return_type='array';
    protected $soft_delete=true;

    protected $before_create=array('created_at');
    protected $before_update=array('update_pre_filter');

    public $validate = array(
        array( 'field' => 'username',
               'rules' => 'required|is_unique[users.username]' ),
        array( 'field' => 'password',
               'rules' => 'required' )
    );

    function __construct()
    {
        parent::__construct();
    }

    //callback when insert row
    function created_at($row){
        $row['ctime']=time();
        return $row;
    }

    function update_pre_filter($row){
        $row['last_login']=time();
        $row['last_login_ip']=get_client_ip();
        if(isset($row['count_login'])){
            $row['count_login']=intval($row['count_login']+1);
        }
        return $row;
    }
}